package com.foxit.pdf2html5.web.foxitcloud.iterface.realization;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.json.JSONException;





import com.foxit.pdf2html5.web.common.Config;
import com.foxit.pdf2html5.common.utils.URLConnectionHelper;
import com.google.gson.Gson;

public class FoxitCloudIterfaceImp  {
	private static final Logger log =  Logger.getLogger(FoxitCloudIterfaceImp.class);
	//获取token
	public static HashMap getToken(final String userId) throws JSONException{
		if(userId == null || "".equals(userId)){
			log.error("getToken method UserId parameter is null");
			return null;
		}
		String tokenUrl = Config.FOXIT_URSER_TOKEN_URL_KEY;
		String appId = Config.FOXIT_APP_ID_KEY;
		if(tokenUrl == null || "".equals(tokenUrl)){
			log.error("getToken method tokenUrl is null");
			return null;
		}
		if(appId == null || "".equals(appId)){
			log.error("getToken method appId is null");
			return null;
		}
		
		Gson gson = new Gson();
		HashMap<String,Object> jsonParam= new HashMap<String,Object>();
		jsonParam.put(FoxitCloudHttpKey.APPID, appId);
		jsonParam.put(FoxitCloudHttpKey.USERID, userId);
		String result = URLConnectionHelper.sendPost(tokenUrl, gson.toJson(jsonParam));
		return  gson.fromJson(result.trim(), HashMap.class);
	}
	
	//获取文档上传分块信息
	public static HashMap getBlockInfo(final String token,final String fileName,final byte[] fileContent,final String fileHash)
			throws Exception {
		if(token == null || "".equals(token)){
			log.error("getBlockInfo method token parameter is null");
			return null;
		}
		String blockInfoUrl = Config.FOXIT_BLOCK_INFO_URL_KEY;
		if(blockInfoUrl == null || "".equals(blockInfoUrl)){
			log.error("getBlockInfo method blockInfoUrl is null");
			return null;
		}
		Gson gson = new Gson();
		HashMap<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put(FoxitCloudHttpKey.TOKEN, token);
		jsonMap.put(FoxitCloudHttpKey.PATH, "/" + fileName);
		jsonMap.put(FoxitCloudHttpKey.DOCSIZE, fileContent.length+"");
		jsonMap.put(FoxitCloudHttpKey.DOCHASH, fileHash);
		jsonMap.put(FoxitCloudHttpKey.DOCFILENAME, fileName);
		jsonMap.put(FoxitCloudHttpKey.DOCID, "");
		//jsonMap.put(FoxitCloudHttpKey.BLOCKSIZE, (Config.GetValue(ConfigConstans.FOXIT_BLOCK_SIZE_MB_KEY, 10)*1024*1024)+"");
		jsonMap.put(FoxitCloudHttpKey.BLOCKSIZE, (20*1024*1024));
		String result = URLConnectionHelper.sendPost(blockInfoUrl, gson.toJson(jsonMap));
		return gson.fromJson(result, HashMap.class);
	}
	
	//分块上传文档
	public static HashMap uploadBlock(HashMap blockInfoMap, final String token, final String fileName, final byte[] fileContent,final String fileHash)
			throws Exception {
		if(token == null || "".equals(token)){
			log.error("uploadBlock method token parameter is null");
			return null;
		}
		
		String docId = (String)blockInfoMap.get(FoxitCloudHttpKey.DOCID);
		if(docId == null || "".equals(docId)){
			log.error("uploadBlock method  docId is null");
			return null;
		}
		blockInfoMap = (HashMap)blockInfoMap.get(FoxitCloudHttpKey.BLOCKINFO);
		if(blockInfoMap == null ){
			log.error("uploadBlock method blockInfoMap is null");
			return null;
		}
		String uploadBlockUrl = Config.FOXIT_BLOCK_UPLOAD_URL_KEY;
		if(uploadBlockUrl == null || "".equals(uploadBlockUrl)){
			log.error("uploadBlock method uploadBlockUrl is null");
			return null;
		}
		String blockId = (String)blockInfoMap.get(FoxitCloudHttpKey.BLOCKID);
		int blockSize = new BigDecimal(blockInfoMap.get(FoxitCloudHttpKey.BLOCKSIZE).toString()).intValue();
		int blockIndex = new BigDecimal(blockInfoMap.get(FoxitCloudHttpKey.BLOCKINDEX).toString()).intValue();
		System.out.println(blockSize);
		System.out.println(blockIndex);
		int blockArrayFrom = blockIndex * blockSize;
		int mathArrayTo = blockArrayFrom + blockSize;
		int blockArrayTo = (mathArrayTo > fileContent.length) ? fileContent.length : mathArrayTo;
		byte[] byteArray = Arrays.copyOfRange(fileContent, blockArrayFrom, blockArrayTo);
		Gson gson = new Gson();
		HashMap<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put(FoxitCloudHttpKey.TOKEN, token);
		jsonMap.put(FoxitCloudHttpKey.DOCID, docId);
		jsonMap.put(FoxitCloudHttpKey.PATH, "/" + Config.FOXIT_USER_ID_KEY + "/"  + fileName);
		jsonMap.put(FoxitCloudHttpKey.BLOCKID, blockId);
		jsonMap.put(FoxitCloudHttpKey.BLOCKSIZE, blockSize);
		jsonMap.put(FoxitCloudHttpKey.BLOCKHASH, fileHash);
		jsonMap.put(FoxitCloudHttpKey.BLOCKDATA, byteArray);
		String result = URLConnectionHelper.sendPost(uploadBlockUrl, gson.toJson(jsonMap));
		return gson.fromJson(result, HashMap.class);
	}
	
	//获取文档分享地址url
	public static HashMap sharesCreate(final String token,final String docId,final String fileName) throws JSONException{
		if(token == null || "".equals(token)){
			log.error("sharesCreate method token parameter is null");
			return null;
		}
		if(docId == null || "".equals(docId)){
			log.error("sharesCreate method docId is null");
			return null;
		}
		String sharesCreateUrl = Config.FOXIT_SHARES_CREATE_URL_KEY;
		if(sharesCreateUrl == null || "".equals(sharesCreateUrl)){
			log.error("sharesCreate method sharesCreateUrl is null");
			return null;
		}
		Gson gson = new Gson();
		HashMap<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put(FoxitCloudHttpKey.TOKEN,token);
		jsonMap.put(FoxitCloudHttpKey.DOCID, docId);
		jsonMap.put(FoxitCloudHttpKey.PATH, "/"+fileName);
		String result = URLConnectionHelper.sendPost(sharesCreateUrl, gson.toJson(jsonMap));
		return gson.fromJson(result, HashMap.class);
	}
	
    //获取目录下文件列表(当前用户)
    //limit	(type string) 返回条目控制，参数格式为：n1-n2。
    //extension 文件括展名，只返回该类型的文档
	public static HashMap getFileList(final String token,final String limit,final String[] extension,final String path){
		if(token == null || "".equals(token)){
			log.error("getFileList method token parameter is null");
			return null;
		}
		Gson gson = new Gson();
		HashMap<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put(FoxitCloudHttpKey.TOKEN, token);
		jsonMap.put(FoxitCloudHttpKey.PATH, path);
		if(limit != null && !"".equals(limit)){
			jsonMap.put(FoxitCloudHttpKey.LIMIT, limit);
		}
		if(extension != null){
			jsonMap.put(FoxitCloudHttpKey.EXTENSION, extension);
		}
		String fileListUrl = Config.FOXIT_CLOUND_URSER_FILE_LIST_KEY;
		if(fileListUrl == null || "".equals(fileListUrl)){
			log.error("fileListUrl  is null");
			return null;
		}
		String result = URLConnectionHelper.sendPost(fileListUrl, gson.toJson(jsonMap));
		HashMap resultMap = gson.fromJson(result, HashMap.class);
		
		
		return resultMap;
	}
	
	//删除单个文档
	public static HashMap deleteFile(final String token,final String docId,final String fileName){
		if(token == null || "".equals(token)){
			log.error("deleteFile method token parameter is null");
			return null;
		}
		if(docId == null || "".equals(docId)){
			log.error("deleteFile method docId parameter is null");
			return null;
		}
		if(fileName == null || "".equals(fileName)){
			log.error("deleteFile method fileName parameter is null");
			return null;
		}
		String deleteFileUrl = Config.FOXIT_CLOUND_DELETE_FILE_URL_KEY;
		if(deleteFileUrl == null || "".equals(deleteFileUrl)){
			log.error("deleteFileUrl is null");
			return null;
		}
		
		Gson gson = new Gson();
		HashMap<String,Object> jsonMap = new HashMap<String,Object>();
		HashMap<String,Object> docListMap = new HashMap<String,Object>();
		docListMap.put(FoxitCloudHttpKey.DOCID,docId);
		docListMap.put(FoxitCloudHttpKey.PATH, "/"+fileName);
		ArrayList arrayList = new ArrayList();
		arrayList.add(docListMap);
		
		jsonMap.put(FoxitCloudHttpKey.TOKEN, token);
		jsonMap.put(FoxitCloudHttpKey.DOCLIST,arrayList);
		
		System.out.println(gson.toJson(jsonMap));
		String result = URLConnectionHelper.sendPost(deleteFileUrl, gson.toJson(jsonMap));
		HashMap resultMap = gson.fromJson(result, HashMap.class);
		return resultMap;
	}
	
	
}
